import {h, ref, unref} from 'vue';
import {NTag, NButton} from 'naive-ui';
import PackageTableItem from "./packageTableItem.vue"

export interface TableItemAction {
  type: string
  action: Function
}

const tableItemAction = ref<TableItemAction[]>([]);


export const columns = [
  {
    type: 'selection',
    key: 'selection',
  },
  {
    title: "ID",
    key: "id",
    width: "50"
  },
  {
    title: '壳包KEY',
    key: 'appKey',
    width: "200"
  },
  {
    title: '壳包名称',
    key: 'appName',
    width: "100"
  },
  {
    title: '游戏名称',
    key: 'gameName',
    width: "150"
  }, {
    title: '红包状态',
    key: 'redStatus',
    width: "80",
    render(row) {
      return h(
        NTag,
        {
          type: row.redStatus == 'able' ? 'success' : 'error',
        },
        {
          default: () => (row.redStatus == 'able' ? '开启' : '关闭'),
        }
      );
    },
  }, {
    title: '渠道ID',
    key: 'chanId',
    width: "180"
  }, {
    title: '下载地址',
    key: 'downUrl',
    width: "120",
    render(row) {
      return h(PackageTableItem, {
        itemValue: row.downUrl
      })
    }
  },
  {
    title: '是否分包',
    key: 'isPack',
    width: "80",
    render(row) {
      return h(
        NTag,
        {
          type: row.isPack == 'able' ? 'success' : 'error',
        },
        {
          default: () => (row.isPack == 'able' ? '开启' : '关闭'),
        }
      );
    },
  },
  {
    title: '壳包状态',
    key: 'appStatus',
    width: "80",
    render(row) {
      return h(
        NTag,
        {
          type: row.appStatus == 'trial' ? 'success' : 'error',
        },
        {
          default: () => (row.appStatus == 'trial' ? '推广中' : '过审中'),
        }
      );
    },
  },
  {
    title: '壳包类型',
    key: 'appType',
    width: "80"
  },
  {
    title: '壳包描述',
    key: 'appDesc',
    width: "80"
  },
  {
    title: "通道名称",
    key: 'chanName',
    width: "100"
  },
  {
    title: '游戏Key',
    key: 'gameKey',
    width: "120"
  },
  {
    title: '创建时间',
    key: 'createTime',
    width: "150"
  },
  {
    title: '更新时间',
    key: 'updateTime',
    width: "150"
  },
];


function actionFun(type: string): Function {
  for (let action of unref(tableItemAction)) {
    if (type === action.type) {
      return action.action;
    }
  }
  return () => {
  }
}


export function registerHandler(actions: TableItemAction[]) {
  tableItemAction.value = actions
}
